package com.example.springboot.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * <p>
 * </p>
 *
 * @author Zero
 * @since 2019/6/12
 */
@RestController
public class ExampleController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/")
    public String welcome() {
        return "Welcome!";
    }

    @GetMapping("uuid")
    public String uuid() {
        String key = "uuid";
        String uuid = UUID.randomUUID().toString();
        stringRedisTemplate.opsForValue().set(key, uuid);
        String result = stringRedisTemplate.opsForValue().get(key);
        return result;
    }

    @GetMapping("random")
    public String random() {
        String key = "random";
        Double dou = Math.random();
        redisTemplate.opsForValue().set(key, dou);
        Double result = (Double) redisTemplate.opsForValue().get(key);
        return result.toString();
    }

}
